package com.chezai.client.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.chezai.client.R;

import java.util.List;

public class BluetoothListAdapter extends BaseAdapter {
    public static final int BIND = 0;
    public static final int UN_BIND = 1;
    private LayoutInflater mInflater;
    private Context mContext;
    private List<BluetoothDevice> mDevices;
    private BindOnClickListener bindOnClickListener;

    public BluetoothListAdapter(Context context, List<BluetoothDevice> devices, BindOnClickListener _bindOnClickListener) {
        this.bindOnClickListener = _bindOnClickListener;
        mContext = context;
        mDevices = devices;
        this.mInflater = LayoutInflater.from(context);
    }
    public void addItem( List<BluetoothDevice> devices){
        mDevices.addAll(devices);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mDevices == null) {
            return 0;
        }
        return mDevices.size();
    }

    @Override
    public Object getItem(int position) {

        return mDevices.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.bluetooth_list_item, null);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
            holder.tvBound = (TextView) convertView.findViewById(R.id.tvBound);
            holder.btBin = (Button) convertView.findViewById(R.id.btBin);
            holder.btUnBound = (Button) convertView.findViewById(R.id.btUnBound);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final BluetoothDevice device = mDevices.get(position);
        if (device.getBondState() == BluetoothDevice.BOND_BONDED){
            holder.btUnBound.setVisibility(View.VISIBLE);
        } else {
            holder.btUnBound.setVisibility(View.GONE);
        }
        holder.tvName.setText(device.getName());
        holder.tvAddress.setText(device.getAddress());
        holder.tvBound.setText((device.getBondState() == BluetoothDevice.BOND_BONDED) ? "已配对" : "未配对");
        holder.btBin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindOnClickListener.choseData(device , BIND);
            }
        });
        holder.btUnBound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindOnClickListener.choseData(device , UN_BIND);
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        private TextView tvName;
        private TextView tvAddress;
        private TextView tvBound;
        private Button btBin;
        private Button btUnBound;
    }
    public interface BindOnClickListener{
        void  choseData(BluetoothDevice device, int clikeType);
    }
}

